// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() => runApp(const MaterialApp(home: WebViewPage()));

class WebViewPage extends StatefulWidget {
  const WebViewPage({Key? key}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  bool isLoading = true; // 设置状态

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize( //解决不设置appbar内容被状态栏遮住问题
        preferredSize:
            Size.fromHeight(MediaQueryData.fromWindow(window).padding.top),
        child: const SafeArea(
          top: true,
          child: Offstage(),
        ),
      ),
//      appBar: AppBar(
//        title: const Text("WebViewDemo"),
//        actions: <Widget>[
//          NavigationControls(_controller.future),
//        ],
//      ),
      body: Stack(
        children: [
          WebView(
            initialUrl: "http://81.70.79.41",
//            initialUrl: "https://youzan.github.io/vant/#/zh-CN/",
//            initialUrl: "https://github.com/smallLabel/loadLocalHtml/blob/main/load_local_html/pubspec.yaml",
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _controller.complete(webViewController);
            },
            navigationDelegate: (NavigationRequest request) {
              var url = request.url;
              // ignore: avoid_print
              print("visit $url");
              setState(() {
                isLoading = true; // 开始访问页面，更新状态
              });

              return NavigationDecision.navigate;
            },
            onPageFinished: (String url) {
              setState(() {
                isLoading = false; // 页面加载完成，更新状态
              });
            },
          ),
          isLoading
              ? const Center(
                  child: CircularProgressIndicator(),
                )
              : Container(),
        ],
      ),
//      floatingActionButton: getFloatingButton(),
    );
  }
}

//class NavigationControls extends StatelessWidget {
//  const NavigationControls(this._webViewControllerFuture)
//      : assert(_webViewControllerFuture != null);
//
//  final Future<WebViewController> _webViewControllerFuture;
//
//  @override
//  Widget build(BuildContext context) {
//    return FutureBuilder<WebViewController>(
//      future: _webViewControllerFuture,
//      builder:
//          (BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
//        final bool webViewReady =
//            snapshot.connectionState == ConnectionState.done;
//        final WebViewController? controller = snapshot.data;
//        return Row(
//          children: <Widget>[
//            IconButton(
//              icon: const Icon(Icons.arrow_back_ios),
//              onPressed: !webViewReady
//                  ? null
//                  : () async {
//                      if (await controller!.canGoBack()) {
//                        await controller.goBack();
//                      } else {
//                        // ignore: deprecated_member_use
//                        Scaffold.of(context).showSnackBar(
//                          const SnackBar(content: Text('No back history item')),
//                        );
//                        return;
//                      }
//                    },
//            ),
//            IconButton(
//              icon: const Icon(Icons.arrow_forward_ios),
//              onPressed: !webViewReady
//                  ? null
//                  : () async {
//                      if (await controller!.canGoForward()) {
//                        await controller.goForward();
//                      } else {
//                        // ignore: deprecated_member_use
//                        Scaffold.of(context).showSnackBar(
//                          const SnackBar(
//                              content: Text('No forward history item')),
//                        );
//                        return;
//                      }
//                    },
//            ),
//            IconButton(
//              icon: const Icon(Icons.replay),
//              onPressed: !webViewReady
//                  ? null
//                  : () {
//                      controller!.reload();
//                    },
//            ),
//          ],
//        );
//      },
//    );
//  }
//}
